{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display Random Streaming data with bokeh line chart and scatter plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pixiedust.display.app import *\n",
    "from pixiedust.display.streaming import *\n",
    "from pixiedust.display.streaming.bokeh import *\n",
    "\n",
    "N = 100\n",
    "class RandomStreamingAdapter(StreamingDataAdapter):\n",
    "    def __init__(self):\n",
    "        self.x = np.random.random(size=N) * 100\n",
    "        self.y = np.random.random(size=N) * 100\n",
    "    \n",
    "    def doGetNextData(self):\n",
    "        rand = 2 * (np.random.random(size=N) - 0.5)\n",
    "        d = np.sqrt((self.x-50)**2 + (self.y-50)**2)/100\n",
    "        self.x = self.x + 2 * np.sin(d) * rand\n",
    "        self.y = self.y + np.cos(d**2) * rand\n",
    "        #return (self.x, self.y)\n",
    "        return self.y\n",
    "\n",
    "@PixieApp\n",
    "class StreamingApp():    \n",
    "    def setup(self):\n",
    "        self.streamingData = RandomStreamingAdapter()\n",
    "        self.scatter = False\n",
    "        self.streamingDisplay = None\n",
    "\n",
    "    def newDisplayHandler(self, options, entity):\n",
    "        if self.streamingDisplay is None:\n",
    "            self.streamingDisplay = ScatterPlotStreamingDisplay(options, entity) if self.scatter else LineChartStreamingDisplay(options, entity)\n",
    "        return self.streamingDisplay\n",
    "        \n",
    "    @route()\n",
    "    def main(self):\n",
    "        return\"\"\"\n",
    "<button type=\"button\" class=\"btn btn-default\">Toggle\n",
    "    <pd_script>\n",
    "self.scatter = not self.scatter\n",
    "self.streamingDisplay = None\n",
    "</pd_script>\n",
    "</button>\n",
    "            <div pd_entity=\"streamingData\" pd_refresh_rate=\"1000\">\n",
    "            </div>\n",
    "        \"\"\"\n",
    "\n",
    "#run the app\n",
    "a = StreamingApp()\n",
    "a.run(runInDialog='false')"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Edit Metadata",
  "kernelspec": {
   "display_name": "pySpark (Spark 1.6.0) Python 2",
   "language": "python",
   "name": "pyspark1.6python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
